
/*
	Before using this do-file, please make sure to:
	
		- Change the target directories from globals $pdata and $Results
		
		- $pdata is directed to a folder containing two databases:
			(i) 	survey_response.dta
			(ii)	analysis_sample.dta
			A description of these databases is available in the Read me files
			
		- $Results is directed to a folder containing two other folders:
			(i) 	Figures
			(ii)	Tables
			Please create these two folders. In thse folders is where the 
			results will be stored
			
		- Download the following user-written programs:
			(i) 	rddensity
			(ii)	lpdensity
			(iii)	svmat2
		
*/


clear all
global pdata 	"C:\Users\mpmar\Desktop\PhD\Projects\RA work - Romanian Tracking Policy\Submission to RESTAT\Replication files\Databases\Processed"
global Results	"C:\Users\mpmar\Desktop\PhD\Projects\RA work - Romanian Tracking Policy\Submission to RESTAT\Replication files\FT, appendix"

global pdata 	"C:\Users\omx9891\Dropbox\Boxifier\Research\romania_schools\replication\"
global Results	"C:\Users\omx9891\Dropbox\Boxifier\Research\romania_schools\replication\"

global pdata 	"C:\Users\oferm\Dropbox\Boxifier\Research\romania_schools\replication\"
global Results 	"C:\Users\oferm\Dropbox\Boxifier\Research\romania_schools\replication\"


********************************************************************************
********************************************************************************
*************************** FIGURES IN APPENDIX ********************************
********************************************************************************
********************************************************************************

********************************************************************************
******************************** FIGURE A1 *************************************
********************************************************************************

	****************************************************************************
	****************************************************************************
	**** Figure A1, Panel A: Likelihood of being in the top class

	*****************************
	**** Setting up the data ****
	*****************************
	
	use "$pdata\analysis_sample.dta", clear

	*SET GLOBAL PARAMETERS
	global bw "0.5"
	global step ".3"
	global end=3.6		// The value of end should be a multiple of step
	global cutwidthA "0($step)$end"
	global cutwidthB "-$end($step)0"

	*CUT MEDIA VARIABLE
	egen media_catA=cut(media_std) if media_std>0, at($cutwidthA)
	egen media_catB=cut(media_std) if media_std<0, at($cutwidthB)

	*gen media_catA=media_std if media_std>0
	*gen media_catB=media_std if media_std<0

	gen 	media_cat=media_catA
	replace media_cat=media_catB if media_cat==.
	replace media_cat=media_cat+($step/2)
	replace media_cat=.15 if media_std==0

	*replace media_cat=2.85 if media_cat>2.84 & media_cat<3.16
	*replace media_cat=-2.55 if media_cat<-2.84 & media_cat>-3.16

	* Make sure we have the same number of points on each side of the threshold
	replace media_cat=2.25 if media_cat>2.24 & media_cat<3.16
	replace media_cat=-2.25 if media_cat<-2.24 & media_cat>-3.16

	*GENERATE TRENDS
	*gen media_std2=media_std*media_std
	*gen media_std3=media_std*media_std2
	global trend "media_std media_std2 media_std3"
	global trend "media_std media_std2"
	global trend "media_std"


	*REGRESS FOR PREDICTED VALUES - All informants

		gen Bbetter_class=better_class if Bs_disrupt==1 & Bs_confid==1 & 			///
									Bs_effort==1 & Bs_ability==1 & Bs_perform==1

	local i=1

	foreach info in i1 i2 i3 i4 i5 i6{
		
		cap reg better_class $trend if media_std<0 & `info'==1, robust
		cap predict better_class_A`i' if e(sample)
		cap predict better_class_A_se`i' if e(sample), stdp
		cap gen better_class_A_low`i' = better_class_A`i' - 1.96*better_class_A_se`i'
		cap gen better_class_A_high`i' = better_class_A`i' + 1.96*better_class_A_se`i'
		
		cap reg better_class $trend if media_std>0 & `info'==1, robust
		cap predict better_class_B`i' if e(sample)
		cap predict better_class_B_se`i' if e(sample), stdp
		cap gen better_class_B_low`i' = better_class_B`i' - 1.96*better_class_B_se`i'
		cap gen better_class_B_high`i' = better_class_B`i' + 1.96*better_class_B_se`i'

		gen s_disrupt`i'= s_disrupt if `info'==1
		cap reg s_disrupt $trend if media_std<0 & `info'==1, robust
		cap predict s_disrupt_A`i' if e(sample)
		cap predict s_disrupt_A_se`i' if e(sample), stdp
		cap gen s_disrupt_A_low`i' = s_disrupt_A`i' - 1.96*s_disrupt_A_se`i'
		cap gen s_disrupt_A_high`i' = s_disrupt_A`i' + 1.96*s_disrupt_A_se`i'
		
		cap reg s_disrupt $trend if media_std>0 & `info'==1, robust
		cap predict s_disrupt_B`i' if e(sample)
		cap predict s_disrupt_B_se`i' if e(sample), stdp
		cap gen s_disrupt_B_low`i' = s_disrupt_B`i' - 1.96*s_disrupt_B_se`i'
		cap gen s_disrupt_B_high`i' = s_disrupt_B`i' + 1.96*s_disrupt_B_se`i'
		
		gen s_confid`i'= s_confid if `info'==1
		cap reg s_confid $trend if media_std<0 & `info'==1, robust
		cap predict s_confid_A`i' if e(sample)
		cap predict s_confid_A_se`i' if e(sample), stdp
		cap gen s_confid_A_low`i' = s_confid_A`i' - 1.96*s_confid_A_se`i'
		cap gen s_confid_A_high`i' = s_confid_A`i' + 1.96*s_confid_A_se`i'
		
		cap reg s_confid $trend if media_std>0 & `info'==1, robust
		cap predict s_confid_B`i' if e(sample)
		cap predict s_confid_B_se`i' if e(sample), stdp
		cap gen s_confid_B_low`i' = s_confid_B`i' - 1.96*s_confid_B_se`i'
		cap gen s_confid_B_high`i' = s_confid_B`i' + 1.96*s_confid_B_se`i'
		
		gen s_effort`i'= s_effort if `info'==1
		cap reg s_effort $trend if media_std<0 & `info'==1, robust
		cap predict s_effort_A`i' if e(sample)
		cap predict s_effort_A_se`i' if e(sample), stdp
		cap gen s_effort_A_low`i' = s_effort_A`i' - 1.96*s_effort_A_se`i'
		cap gen s_effort_A_high`i' = s_effort_A`i' + 1.96*s_effort_A_se`i'
		
		cap reg s_effort $trend if media_std>0 & `info'==1, robust
		cap predict s_effort_B`i' if e(sample)
		cap predict s_effort_B_se`i' if e(sample), stdp
		cap gen s_effort_B_low`i' = s_effort_B`i' - 1.96*s_effort_B_se`i'
		cap gen s_effort_B_high`i' = s_effort_B`i' + 1.96*s_effort_B_se`i'

		gen s_ability`i'= s_ability if `info'==1
		cap reg s_ability $trend if media_std<0 & `info'==1, robust
		cap predict s_ability_A`i' if e(sample)
		cap predict s_ability_A_se`i' if e(sample), stdp
		cap gen s_ability_A_low`i' = s_ability_A`i' - 1.96*s_ability_A_se`i'
		cap gen s_ability_A_high`i' = s_ability_A`i' + 1.96*s_ability_A_se`i'
		
		cap reg s_ability $trend if media_std>0 & `info'==1, robust
		cap predict s_ability_B`i' if e(sample)
		cap predict s_ability_B_se`i' if e(sample), stdp
		cap gen s_ability_B_low`i' = s_ability_B`i' - 1.96*s_ability_B_se`i'
		cap gen s_ability_B_high`i' = s_ability_B`i' + 1.96*s_ability_B_se`i'

		gen s_perform`i'= s_perform if `info'==1
		cap reg s_perform $trend if media_std<0 & `info'==1, robust
		cap predict s_perform_A`i' if e(sample)
		cap predict s_perform_A_se`i' if e(sample), stdp
		cap gen s_perform_A_low`i' = s_perform_A`i' - 1.96*s_perform_A_se`i'
		cap gen s_perform_A_high`i' = s_perform_A`i' + 1.96*s_perform_A_se`i'
		
		cap reg s_perform $trend if media_std>0 & `info'==1, robust
		cap predict s_perform_B`i' if e(sample)
		cap predict s_perform_B_se`i' if e(sample), stdp
		cap gen s_perform_B_low`i' = s_perform_B`i' - 1.96*s_perform_B_se`i'
		cap gen s_perform_B_high`i' = s_perform_B`i' + 1.96*s_perform_B_se`i'
		
		gen bac_rank`i'= bac_rank if `info'==1 
		cap reg bac_rank $trend if media_std<0 & `info'==1, robust
		cap predict bac_rank_A`i' if e(sample)
		cap predict bac_rank_A_se`i' if e(sample), stdp
		cap gen bac_rank_A_low`i' = bac_rank_A`i' - 1.96*bac_rank_A_se`i'
		cap gen bac_rank_A_high`i' = bac_rank_A`i' + 1.96*bac_rank_A_se`i'
		
		cap reg bac_rank $trend if media_std>0 & `info'==1, robust
		cap predict bac_rank_B`i' if e(sample)
		cap predict bac_rank_B_se`i' if e(sample), stdp
		cap gen bac_rank_B_low`i' = bac_rank_B`i' - 1.96*bac_rank_B_se`i'
		cap gen bac_rank_B_high`i' = bac_rank_B`i' + 1.96*bac_rank_B_se`i'
		
		*gen bac_ranku`i'= bac_ranku if `info'==1
		*cap reg bac_ranku $trend if media_std<0 & `info'==1, robust
		*cap predict bac_ranku_A`i' if e(sample)
		*cap predict bac_ranku_A_se`i' if e(sample), stdp
		*cap gen bac_ranku_A_low`i' = bac_ranku_A`i' - 1.96*bac_ranku_A_se`i'
		*cap gen bac_ranku_A_high`i' = bac_ranku_A`i' + 1.96*bac_ranku_A_se`i'

		*cap reg bac_ranku $trend if media_std>0 & `info'==1, robust
		*cap predict bac_ranku_B`i' if e(sample)
		*cap predict bac_ranku_B_se`i' if e(sample), stdp
		*cap gen bac_ranku_B_low`i' = bac_ranku_B`i' - 1.96*bac_ranku_B_se`i'
		*cap gen bac_ranku_B_high`i' = bac_ranku_B`i' + 1.96*bac_ranku_B_se`i'
		

	local i = `i'+1	

	}
	
	collapse Bbetter_class better_class* s_disrupt* s_confid* s_effort* s_ability* s_perform* bac_rank*	///
				(count) i1 i2 i3 i4 i5 i6, by(media_cat)

	replace media_cat =. if media_cat ==0

	***************************************************
	**** Figure, disruptiveness - self-perceptions ****
	***************************************************
	
	#delimit;

	twoway 	(scatter s_disrupt4 media_cat, msymbol(Oh) msize(small) mcolor(blue) sort) 			|| 
			(lfit s_disrupt_A4 media_cat, clcolor(blue) clwidth(thick) clpattern(solid))		||
			(lfit s_disrupt_B4 media_cat, clcolor(blue) clwidth(thick) clpattern(solid))		||
			(scatter s_disrupt5 media_cat, msymbol(Oh) msize(small) mcolor(gs4) sort) 			|| 
			(lfit s_disrupt_A5 media_cat, clcolor(gs4) clwidth(thin) clpattern(solid))			||
			(lfit s_disrupt_B5 media_cat, clcolor(gs4) clwidth(thin) clpattern(solid)),
			 legend(off)
			 xlabel(-2.5(1)2.5, labsize(medium))
			 ylabel(2(2)10, labsize(medium))
			 title(Disruptiveness, position(11) size(medlarge))
			 ytitle(Average rank, size(medium))
			 xtitle(Normed transition score, size(medium))
			 xline(0)
			 scheme(s1color)
			saving("${Results}\Figures\FigureA1A.gph",replace);

	#delimit cr	
	
	graph export "${Results}\Figures\FigureA1A.png", as(png) replace	
	graph export "${Results}\Figures\FigureA1A.eps", as(eps) name("Graph") preview(off) replace
 	
	
	***************************************************
	**** Figure, disruptiveness - peer perceptions ****
	***************************************************
	
	#delimit;

	twoway 	(scatter s_disrupt2 media_cat, msymbol(Oh) msize(small) mcolor(green) sort) 		|| 
			(lfit s_disrupt_A2 media_cat, clcolor(green) clwidth(thick) clpattern(shortdash))	||
			(lfit s_disrupt_B2 media_cat, clcolor(green) clwidth(thick) clpattern(shortdash))	||
			(scatter s_disrupt3 media_cat, msymbol(Oh) msize(small) mcolor(red) sort) 			|| 
			(lfit s_disrupt_A3 media_cat, clcolor(red) clwidth(thick) clpattern(longdash))		||
			(lfit s_disrupt_B3 media_cat, clcolor(red) clwidth(thick) clpattern(longdash))		||
			(scatter s_disrupt5 media_cat, msymbol(Oh) msize(small) mcolor(gs4) sort) 			|| 
			(lfit s_disrupt_A5 media_cat, clcolor(gs4) clwidth(thin) clpattern(solid))			||
			(lfit s_disrupt_B5 media_cat, clcolor(gs4) clwidth(thin) clpattern(solid)),
			 legend(off)
			 xlabel(-2.5(1)2.5, labsize(medium))
			 ylabel(2(2)10, labsize(medium))
			 title(Disruptiveness, position(11) size(medlarge))
			 ytitle(Average rank, size(medium))
			 xtitle(Normed transition score, size(medium))
			 xline(0)
			 scheme(s1color)
			saving("${Results}\Figures\FigureA1B.gph",replace);

	#delimit cr	
	
	graph export "${Results}\Figures\FigureA1B.png", as(png) replace	
	graph export "${Results}\Figures\FigureA1B.eps", as(eps) name("Graph") preview(off) replace
 	

	
	****************************************************************************
	****************************************************************************
	**** Figure A1: Combining Panels A and B
	
	* title("Figure A1: RD estimates of disruptiveness: Self-, peer-, and teacher perceptions",	pos(6) size(small))
					
	graph combine  	"${Results}\Figures\FigureA1A.gph" "${Results}\Figures\FigureA1B.gph", 						///
					ysize(5) holes(3 6 9) scheme(s1color) graphregion(c(white) margin(7 7 20 20))  				///
					saving("${Results}\Figures\FigureA1_AllPanels.gph",replace)
				

				
********************************************************************************
********************************* FIGURE A2 ************************************
********************************************************************************
	
	****************************************************
	**** Figure, BAC performance - self-perceptions ****
	****************************************************
	
	#delimit;

	twoway 	(scatter s_perform4 media_cat, msymbol(Oh) msize(small) mcolor(blue) sort) 			|| 
			(lfit s_perform_A4 media_cat, clcolor(blue) clwidth(thick) clpattern(solid))		||
			(lfit s_perform_B4 media_cat, clcolor(blue) clwidth(thick) clpattern(solid))		||
			(scatter bac_rank4 media_cat, msymbol(Oh) msize(small) mcolor(sand) sort) 			|| 
			(lfit bac_rank_A4 media_cat, clcolor(sand) clwidth(thick) clpattern(solid))			||
			(lfit bac_rank_B4 media_cat, clcolor(sand) clwidth(thick) clpattern(solid))			||
			(scatter s_perform5 media_cat, msymbol(Oh) msize(small) mcolor(gs4) sort) 			|| 
			(lfit s_perform_A5 media_cat, clcolor(gs4) clwidth(thin) clpattern(solid))			||
			(lfit s_perform_B5 media_cat, clcolor(gs4) clwidth(thin) clpattern(solid)),
			 legend(off)
			 xlabel(-2.5(1)2.5, labsize(medium))
			 ylabel(2(2)10, labsize(medium))
			 title(Academic Performance, position(11) size(medlarge))
			 ytitle(Average rank, size(medium))
			 xtitle(Normed transition score, size(medium))
			 xline(0)
			 scheme(s1color)
			saving("${Results}\Figures\FigureA2A.gph",replace);

	#delimit cr	
	
	graph export "${Results}\Figures\FigureA2A.png", as(png) replace	
	graph export "${Results}\Figures\FigureA2A.eps", as(eps) name("Graph") preview(off) replace
 	
	
	****************************************************
	**** Figure, BAC performance - peer perceptions ****
	****************************************************
	
	#delimit;

	twoway 	(scatter s_perform2 media_cat, msymbol(Oh) msize(small) mcolor(green) sort) 		|| 
			(lfit s_perform_A2 media_cat, clcolor(green) clwidth(thick) clpattern(shortdash))	||
			(lfit s_perform_B2 media_cat, clcolor(green) clwidth(thick) clpattern(shortdash))	||
			(scatter s_perform3 media_cat, msymbol(Oh) msize(small) mcolor(red) sort) 			|| 
			(lfit s_perform_A3 media_cat, clcolor(red) clwidth(thick) clpattern(longdash))		||
			(lfit s_perform_B3 media_cat, clcolor(red) clwidth(thick) clpattern(longdash))		||
			(scatter bac_rank4 media_cat, msymbol(Oh) msize(small) mcolor(sand) sort) 			|| 
			(lfit bac_rank_A4 media_cat, clcolor(sand) clwidth(thick) clpattern(solid))			||
			(lfit bac_rank_B4 media_cat, clcolor(sand) clwidth(thick) clpattern(solid))			||
			(scatter s_perform5 media_cat, msymbol(Oh) msize(small) mcolor(gs4) sort) 			|| 
			(lfit s_perform_A5 media_cat, clcolor(gs4) clwidth(thin) clpattern(solid))			||
			(lfit s_perform_B5 media_cat, clcolor(gs4) clwidth(thin) clpattern(solid)),
			 legend(off)
			 xlabel(-2.5(1)2.5, labsize(medium))
			 ylabel(2(2)10, labsize(medium))
			 title(Academic Performance, position(11) size(medlarge))
			 ytitle(Average rank, size(medium))
			 xtitle(Normed transition score, size(medium))
			 xline(0)
			 scheme(s1color)
			saving("${Results}\Figures\FigureA2B.gph",replace);

	#delimit cr	
	
	graph export "${Results}\Figures\FigureA2B.png", as(png) replace	
	graph export "${Results}\Figures\FigureA2B.eps", as(eps) name("Graph") preview(off) replace
 	

	
	****************************************************************************
	****************************************************************************
	**** Figure A2: Combining Panels A and B
	
	* title("Figure A2: RD estimates of actual Baccalaureate performance", pos(6) size(small)) 
					
	graph combine  	"${Results}\Figures\FigureA2A.gph" "${Results}\Figures\FigureA2B.gph", 						///
					ysize(5) holes(3 6 9) scheme(s1color) graphregion(c(white) margin(7 7 20 20))  				///
					saving("${Results}\Figures\FigureA2_AllPanels.gph",replace)
	
	
	
********************************************************************************
********************************************************************************
************************** TABLES IN APPENDIX A ********************************
********************************************************************************
********************************************************************************

	** Loading the data
	
	use "$pdata\analysis_sample.dta", clear

	** Control variables

		global control_vars				"i.girl_reporting age_child2_reporting i.nation_reporting i.siblings_reporting i.mom_ed_reporting i.alphabet"
		global control_vars_1			"i.girl_reporting age_child2_reporting i.nation_reporting i.siblings_reporting i.alphabet"
		global control_vars_ranked		"i.girl age_child2 i.nation i.siblings i.mom_ed i.alphabet"
		global control_vars_ranking 	"i.girl_reporting age_child2_reporting i.nation_reporting i.siblings_reporting i.mom_ed_reporting i.alphabet_reporting"

		
********************************************************************************
******************************** TABLE A1 **************************************
********************************************************************************

**** Table A1: Correlations between student perceptions

	pwcorr s_confid s_effort s_ability s_perform s_disrupt if informant=="STUDENTS"
	*star(5)
	matrix Cr1 = r(C)
	pwcorr s_confid s_effort s_ability s_perform s_disrupt if informant=="STUDENTS" & above==0
	matrix Cr2 = r(C)
	pwcorr s_confid s_effort s_ability s_perform s_disrupt if informant=="STUDENTS" & above==1
	matrix Cr3 = r(C)

forvalues m=1/3{	
	preserve 
		svmat2 Cr`m'
		keep if Cr`m'1!=.
		keep Cr`m'*
			
		export excel using "${Results}\Tables\TableA1_`m'.xls", firstrow(var) replace

	restore
}	


********************************************************************************
******************************** TABLE A2 **************************************
********************************************************************************

**** Table A2: Rank differences by gender 

matrix T2 = J(5,5,.)

local n = 1
foreach v of varlist s_confid s_effort s_ability s_perform s_disrupt{
	
	sum `v' if self_report_==1
		matrix T2[`n',1] = r(mean)
	
	ttest `v' if self_report_==1, by(girl)
		matrix T2[`n',2] = r(mu_2)
		matrix T2[`n',3] = r(mu_1)
		matrix T2[`n',4] = r(mu_2) - r(mu_1)
		matrix T2[`n',5] = r(p)
		
local n = `n' + 1
}


preserve 
	svmat2 T2
	keep if T21!=.
	keep T2*
	
	export excel using "${Results}\Tables\TableA2.xls", firstrow(var) replace
restore


********************************************************************************
******************************** TABLE A3 **************************************
********************************************************************************

**** Table A3: Summary statistics for student survey

tab mom_ed, gen(mom_ed)
tab dad_ed, gen(dad_ed)

global sum_vars1 "girl age_child2 nation siblings old_sibs age_mom2 mom_ed1 mom_ed2 mom_ed3 age_dad2 dad_ed1 dad_ed2 dad_ed3"
global sum_vars2 "s_confid s_effort s_ability s_perform s_disrupt"
global sum_vars3 "rosen_std bullying1c_std took_peer_bac bac_peer_raw bac_rank"
*bullying2b bullying2a

matrix TA3a = J(14,9,.)
matrix TA3b = J(5,9,.)
matrix TA3c = J(5,9,.)

local nr = 1
foreach v in $sum_vars1{
	
	sum `v' if (reporter_higher==1 | reporter_lower==1) & self_report_==1
	matrix TA3a[`nr',1] = r(N)
	matrix TA3a[`nr',2] = r(mean)
	matrix TA3a[`nr',3] = r(sd)
	
	sum `v' if (reporter_higher==1) & self_report_==1
	matrix TA3a[`nr',4] = r(N)
	matrix TA3a[`nr',5] = r(mean)
	matrix TA3a[`nr',6] = r(sd)
	
	sum `v' if (reporter_lower==1) & self_report_==1
	matrix TA3a[`nr',7] = r(N)
	matrix TA3a[`nr',8] = r(mean)
	matrix TA3a[`nr',9] = r(sd)
	
	local nr = `nr' + 1
	
}	

preserve 
	svmat2 TA3a
	keep if TA3a1!=.
	keep TA3a*
	
	export excel using "${Results}\Tables\TableA3a.xls", firstrow(var) replace
restore


local nr = 1
foreach v in $sum_vars2{
	
	sum `v' if (reporter_higher==1 | reporter_lower==1) & self_report_==1
	matrix TA3b[`nr',1] = r(N)
	matrix TA3b[`nr',2] = r(mean)
	matrix TA3b[`nr',3] = r(sd)
	
	sum `v' if (reporter_higher==1) & self_report_==1
	matrix TA3b[`nr',4] = r(N)
	matrix TA3b[`nr',5] = r(mean)
	matrix TA3b[`nr',6] = r(sd)
	
	sum `v' if (reporter_lower==1) & self_report_==1
	matrix TA3b[`nr',7] = r(N)
	matrix TA3b[`nr',8] = r(mean)
	matrix TA3b[`nr',9] = r(sd)
	
	local nr = `nr' + 1
	
}	

preserve 
	svmat2 TA3b
	keep if TA3b1!=.
	keep TA3b*
	
	export excel using "${Results}\Tables\TableA3b.xls", firstrow(var) replace
restore


local nr = 1
foreach v in $sum_vars3{
	
	sum `v' if (reporter_higher==1 | reporter_lower==1) & self_report_==1
	matrix TA3c[`nr',1] = r(N)
	matrix TA3c[`nr',2] = r(mean)
	matrix TA3c[`nr',3] = r(sd)
	
	sum `v' if (reporter_higher==1) & self_report_==1
	matrix TA3c[`nr',4] = r(N)
	matrix TA3c[`nr',5] = r(mean)
	matrix TA3c[`nr',6] = r(sd)
	
	sum `v' if (reporter_lower==1) & self_report_==1
	matrix TA3c[`nr',7] = r(N)
	matrix TA3c[`nr',8] = r(mean)
	matrix TA3c[`nr',9] = r(sd)
	
	local nr = `nr' + 1
	
}	

preserve 
	svmat2 TA3c
	keep if TA3c1!=.
	keep TA3c*
	
	export excel using "${Results}\Tables\TableA3c.xls", firstrow(var) replace
restore


*** t-tests and pr-tests

global ttest_cont "age_child2 siblings old_sibs age_mom2 age_dad2 s_confid s_effort s_ability s_perform s_disrupt rosen_std bullying1c_std bac_peer_raw bac_rank"
global prtest_dum "girl nation mom_ed1 mom_ed2 mom_ed3 dad_ed1 dad_ed2 dad_ed3 took_peer_bac"

matrix TA3d = J(23,2,.)

local n = 1
foreach v of varlist $ttest_cont{
	
	ttest `v' if self_report_==1, by(above)
		matrix TA3d[`n',1] = r(mu_2) - r(mu_1)
		matrix TA3d[`n',2] = r(p)
		
local n = `n' + 1
}

local n = 15
foreach v of varlist $prtest_dum{
	
	prtest `v' if self_report_==1, by(above)
		matrix TA3d[`n',1] = r(P2) - r(P1)
		matrix TA3d[`n',2] = r(p)

local n = `n' + 1		
}	

preserve 
	svmat2 TA3d
	keep if TA3d1!=.
	keep TA3d*
	
	export excel using "${Results}\Tables\TableA3d.xls", firstrow(var) replace
restore


********************************************************************************
******************************** TABLE A4 **************************************
********************************************************************************

**** Table A4: Teacher summary statistics

	egen id_prof_tag = tag(id_prof)
	gen __=.

	tabstat t_female t_age t_children __ t_marstat* __ t_ethnic_* __ t_educ_* if id_prof_tag==1, statistic(n mean sd) save
	matrix SUMSTAT = r(StatTotal)'
	xml_tab SUMSTAT, save("${Results}\Tables\TableA4a.xml") t(Teacher demographics) replace

	tabstat t_experience t_cert_* __ t_titular t_suplinitor __ t_subj_* if id_prof_tag==1, statistic(n mean sd) save
	matrix SUMSTAT = r(StatTotal)'
	xml_tab SUMSTAT, save("${Results}\Tables\TableA4b.xml") t(Teacher qualifications) replace

	tabstat t_groups* __ t_homework_* __ t_target_* if id_prof_tag==1, statistic(n mean sd) save
	matrix SUMSTAT = r(StatTotal)'
	xml_tab SUMSTAT, save("${Results}\Tables\TableA4c.xml") t(Teacher teaching characteristics) replace


********************************************************************************
******************************** TABLE A5 **************************************
********************************************************************************

**** Table A5: Continuity checks (in control variables)
	
	local TableA5 "${Results}\Tables\TableA5"
	local TableA5 "${Results}\Tables\TableA5"
	
	cap erase "`TableA5'.xls"
	cap erase "`TableA5'.txt"

	foreach var of varlist age_child2 girl nation siblings mom_ed dad_ed alphabet {
		
		areg `var' above media_std above_media_std if self_report_==1, absorb(id_liceu_disc) robust cluster(id_child2) 
		outreg2 using "${Results}\Tables\TableA5.xls", se bracket coefastr bdec(4) sdec(4) excel slow(2000) append
				
	}


********************************************************************************
******************************** TABLE A6 **************************************
********************************************************************************

**** Table A6: RD estimates of students’ non-response

	** Loading the data
	
	use "${pdata}\survey_response.dta", clear
	
	local TableA6 "${Results}\Tables\TableA6"
	
	cap erase "`TableA6'.xls"
	cap erase "`TableA6'.txt"

		reg rsurvey above media_std above_media_std if IDC==1, robust
		quietly summ rsurvey if e(sample)==1
		outreg2 using "${Results}\Tables\TableA6.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
	
	foreach var in response1 response2 response3 response4 response5 {

		areg any_`var' above media_std above_media_std if IDC==1, robust abs(id_discont) cluster(id_child)
		quietly summ any_`var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableA6.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
						
	}	
	
	
********************************************************************************
******************************** TABLE A7 **************************************
********************************************************************************

**** Loading the data
	
	use "$pdata\analysis_sample.dta", clear

**** Table A7: Differences in reporting by top and bottom class students

	gen 	informants14_new = informants14
	replace informants14_new = 0 if informants==2

	gen 	informants24_new = informants24
	replace informants24_new = 0 if informants==1
		
	gen 	informants124_new = 0 if informants==4
	replace informants124_new = 1 if informants==1
	replace informants124_new = 2 if informants==2
			
	local TableA7a "${Results}\Tables\TableA7a"
	local TableA7b "${Results}\Tables\TableA7b"
	
	cap erase "`TableA7a'.xls"
	cap erase "`TableA7a'.txt"

	cap erase "`TableA7b'.xls"
	cap erase "`TableA7b'.txt"

	foreach var of varlist better_class s_confid s_effort s_ability s_perform s_disrupt {
		** Panel A: Top and bottom vs Teachers
		areg `var' informants14_new informants24_new if above==1 & (self_report_==0 | informant=="TEACHERS"), absorb(id_child) robust cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableA7a.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
			
		* Panel B: Top and bottom vs Teachers
		areg `var' informants14_new informants24_new if above==0 & (self_report_==0 | informant=="TEACHERS"), absorb(id_child) robust cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableA7b.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
	}
		

********************************************************************************
******************************** TABLE A8 **************************************
********************************************************************************

**** Table A8: RD estimates of Baccalaureate outcomes


**** Absolute measures of student's perceptions
		
	local TableA8a "${Results}\Tables\TableA8a"
	
	cap erase "`TableA8a'.xls"
	cap erase "`TableA8a'.txt"
	
	foreach var of varlist took_peer_bac bac_peer_raw bac_rank {

		areg `var' above media_std above_media_std if self_report_==1, robust absorb(id_liceu_disc) cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableA8a.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
				
	}		
	
**** Absolute measures of student's perceptions including control variables
		
	local TableA8b "${Results}\Tables\TableA8b"
	
	cap erase "`TableA8b'.xls"
	cap erase "`TableA8b'.txt"
	
	foreach var of varlist took_peer_bac bac_peer_raw bac_rank {

		areg `var' above media_std above_media_std $control_vars if self_report_==1, robust absorb(id_liceu_disc) cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableA8b.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
		
	}

**** Teacher performance rank - No controls
	areg s_perform above media_std above_media_std if informants==4, absorb(id_liceu_disc) robust cluster(id_child2) 
	quietly summ s_perform if e(sample)==1
	outreg2 using "${Results}\Tables\TableA8a.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
	
	
**** Teacher performance rank - Including controls
	areg s_perform above media_std above_media_std i.alphabet if informants==4, absorb(id_liceu_disc) robust cluster(id_child2) 
	quietly summ s_perform if e(sample)==1
	outreg2 using "${Results}\Tables\TableA8b.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append



********************************************************************************
********************************************************************************
************************** TABLES IN APPENDIX B ********************************
********************************************************************************
********************************************************************************


********************************************************************************
******************************** TABLE B1 **************************************
********************************************************************************

**** Table B1: Conditional RD estimates of self-perceptions and teacher perceptions

	local Table1a "${Results}\Tables\TableB1a"
	local Table1b "${Results}\Tables\TableB1b"
	
	cap erase "`TableB1a'.xls"
	cap erase "`TableB1a'.txt"

	cap erase "`TableB1b'.xls"
	cap erase "`TableB1b'.txt"
	
	foreach var of varlist better_class s_confid s_effort s_ability s_perform s_disrupt {
		* Panel A
		areg `var' above media_std above_media_std $control_vars if self_report_==1, robust absorb(id_liceu_disc) cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB1a.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
		
		* Panel B
		areg `var' above media_std above_media_std i.alphabet if informants==4, absorb(id_liceu_disc) robust cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB1b.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
		
	}
	
**** Absolute measures of student's perceptions
				
	foreach var of varlist rosen_std bullying1c_std {
		* Panel A, columns (7) and (8)
		areg `var' above media_std above_media_std $control_vars if self_report_==1, robust absorb(id_liceu_disc) cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB1a.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
		
	}



********************************************************************************
******************************** TABLE B2 **************************************
********************************************************************************

**** Table B2: Conditional RD estimates of peer perceptions

	local TableB2a "${Results}\Tables\TableB2a"
	local TableB2b "${Results}\Tables\TableB2b"
	
	cap erase "`TableB2a'.xls"
	cap erase "`TableB2a'.txt"

	cap erase "`TableB2b'.xls"
	cap erase "`TableB2b'.txt"

	foreach var of varlist better_class s_confid s_effort s_ability s_perform s_disrupt {
		* Panel A
		areg `var' above media_std above_media_std $control_vars if reporter_higher==1 & self_report_==0, absorb(id_liceu_disc) robust cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB2a.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
		
		* Panel B
		areg `var' above media_std above_media_std $control_vars if reporter_lower==1 & self_report_==0, absorb(id_liceu_disc) robust cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB2b.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
		
	}		

	
********************************************************************************
******************************** TABLE B3 **************************************
********************************************************************************
	
**** Table B3: Quadratic RD estimates of self-perceptions and teacher perceptions

	local TableB3a "${Results}\Tables\TableB3a"
	local TableB3b "${Results}\Tables\TableB3b"
	
	cap erase "`TableB3a'.xls"
	cap erase "`TableB3a'.txt"

	cap erase "`TableB3b'.xls"
	cap erase "`TableB3b'.txt"
	
	foreach var of varlist better_class s_confid s_effort s_ability s_perform s_disrupt {
		* Panel A
		areg `var' above media_std media_std2 above_media_std above_media_std2 if self_report_==1, robust absorb(id_liceu_disc) cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB3a.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
		
		* Panel B
		areg `var' above media_std media_std2 above_media_std above_media_std2 if informants==4, absorb(id_liceu_disc) robust cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB3b.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
		
	}	
	
**** Absolute measures of student's perceptions
	
	foreach var of varlist rosen_std bullying1c_std {
		* Panel A, columns (7) and (8)
		areg `var' above media_std media_std2 above_media_std above_media_std2 if self_report_==1, robust absorb(id_liceu_disc) cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB3a.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
				
	}		
	
	
********************************************************************************
******************************** TABLE B4 **************************************
********************************************************************************

**** Table B4: Quadratic RD estimates of peer perceptions

	local TableB4a "${Results}\Tables\TableB4a"
	local TableB4b "${Results}\Tables\TableB4b"
	
	cap erase "`TableB4a'.xls"
	cap erase "`TableB4a'.txt"

	cap erase "`TableB4b'.xls"
	cap erase "`TableB4b'.txt"

	foreach var of varlist better_class s_confid s_effort s_ability s_perform s_disrupt {
		* Panel A
		areg `var' above media_std media_std2 above_media_std above_media_std2 if reporter_higher==1 & self_report_==0, absorb(id_liceu_disc) robust cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB4a.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
		
		* Panel B
		areg `var' above media_std media_std2 above_media_std above_media_std2 if reporter_lower==1 & self_report_==0, absorb(id_liceu_disc) robust cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB4b.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
		
	}	
	
	

********************************************************************************
******************************** TABLE B5 **************************************
********************************************************************************

**** Table B5: Self reported vs teacher rankings, different bandwidths

tab id_liceu_disc, gen(d)

encode id_child2, gen(id_child2n)
encode id_liceu_disc, gen(id_liceu_discn)

foreach var of varlist better_class s_confid s_effort s_ability s_perform s_disrupt rosen_std bullying1c_std {
	reg `var' i.id_liceu_discn if self_report_==1, nocons
	predict `var'_dfe1
	
	reg `var' i.id_liceu_discn if informants==4, nocons
	predict `var'_dfe2
}

matrix B 	= J(9,14,.) 

	local TableB5a_bw1 "${Results}\Tables\TableB5a_bw1"
	local TableB5b_bw1 "${Results}\Tables\TableB5b_bw1"
	
	cap erase "`TableB5a_bw1'.xls"
	cap erase "`TableB5a_bw1'.txt"

	cap erase "`TableB5b_bw1'.xls"
	cap erase "`TableB5b_bw1'.txt"
	
	local TableB5a_bw2 "${Results}\Tables\TableB5a_bw2"
	local TableB5b_bw2 "${Results}\Tables\TableB5b_bw2"
	
	cap erase "`TableB5a_bw2'.xls"
	cap erase "`TableB5a_bw2'.txt"

	cap erase "`TableB5b_bw2'.xls"
	cap erase "`TableB5b_bw2'.txt"
	
	local TableB5a_bw3 "${Results}\Tables\TableB5a_bw3"
	local TableB5b_bw3 "${Results}\Tables\TableB5b_bw3"
	
	cap erase "`TableB5a_bw3'.xls"
	cap erase "`TableB5a_bw3'.txt"

	cap erase "`TableB5b_bw3'.xls"
	cap erase "`TableB5b_bw3'.txt"	
	
	local nreg = 1

	foreach var of varlist better_class s_confid s_effort s_ability s_perform s_disrupt {
		** Panel A
		* Bandwidth 1: -1.5 to 1.5
		areg `var' above media_std above_media_std if self_report_==1 & media_std>=-1.5 & media_std<=1.5, robust absorb(id_liceu_disc) cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB5a_bw1.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
		
		* Bandwidth 2: -1.0 to 1.0
		areg `var' above media_std above_media_std if self_report_==1 & media_std>=-1 & media_std<=1, robust absorb(id_liceu_disc) cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB5a_bw2.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
		
		* Bandwidth 3: -0.5 to 0.5
		areg `var' above media_std above_media_std if self_report_==1 & media_std>=-.5 & media_std<=.5, robust absorb(id_liceu_disc) cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB5a_bw3.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append

		* CCT optimal bandwidth
		rdrobust `var' media_std if self_report_==1, bwselect(mserd) all kernel(tri) vce(cluster id_child2n) covs(`var'_dfe1)
		matrix B[1,`nreg'] =  e(tau_cl)
		*matrix B[2,`nreg'] =  e(se_tau_rb)
		matrix B[2,`nreg'] =  e(se_tau_cl)
		matrix B[3,`nreg'] =  e(pv_rb)
		*matrix B[3,`nreg'] =  e(pv_cl)
		matrix B[4,`nreg'] =  e(h_l)
		matrix B[5,`nreg'] =  e(N_h_l)
		matrix B[6,`nreg'] =  e(N_h_r) 
		qui tab id_child2 if media_std>=-e(b_r) & media_std<0
		matrix B[7,`nreg'] =  r(r)
		qui tab id_child2n if media_std>=0 & media_std<e(b_r)
		matrix B[8,`nreg'] =  r(r)
		sum `var' if media_std<=0 & media_std>=-e(h_r)
		matrix B[9,`nreg'] =  r(mean)
		
		
		** Panel B
		* Bandwidth 1: -1.5 to 1.5
		areg `var' above media_std above_media_std if informants==4 & media_std>=-1.5 & media_std<=1.5, absorb(id_liceu_disc) robust cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB5b_bw1.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
		
		* Bandwidth 2: -1.0 to 1.0
		areg `var' above media_std above_media_std if informants==4 & media_std>=-1 & media_std<=1, absorb(id_liceu_disc) robust cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB5b_bw2.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
		
		* Bandwidth 3: -0.5 to 0.5
		areg `var' above media_std above_media_std if informants==4 & media_std>=-.5 & media_std<=.5, absorb(id_liceu_disc) robust cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB5b_bw3.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append

		* CCT optimal bandwidth
		rdrobust `var' media_std if informants==4, bwselect(mserd) all kernel(tri) vce(cluster id_child2n) covs(`var'_dfe2)
		matrix B[1,8+`nreg'] =  e(tau_cl)
		*matrix B[2,8+`nreg'] =  e(se_tau_rb)
		matrix B[2,8+`nreg'] =  e(se_tau_cl)
		matrix B[3,8+`nreg'] =  e(pv_rb)
		*matrix B[3,8+`nreg'] =  e(pv_cl)
		matrix B[4,8+`nreg'] =  e(h_l)
		matrix B[5,8+`nreg'] =  e(N_h_l)
		matrix B[6,8+`nreg'] =  e(N_h_r) 
		qui tab id_child2 if media_std>=-e(b_r) & media_std<0
		matrix B[7,8+`nreg'] =  r(r)
		qui tab id_child2n if media_std>=0 & media_std<e(b_r)
		matrix B[8,8+`nreg'] =  r(r)
		sum `var' if media_std<=0 & media_std>=-e(h_r)
		matrix B[9,8+`nreg'] =  r(mean)
		
	local nreg = `nreg' + 1
	
	}	
	
	
**** Absolute measures of student's perceptions
	
	local nreg = 7

	foreach var of varlist rosen_std bullying1c_std {
		* Bandwidth 1: -1.5 to 1.5
		areg `var' above media_std above_media_std if self_report_==1 & media_std>=-1.5 & media_std<=1.5, robust absorb(id_liceu_disc) cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB5a_bw1.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
		
		* Bandwidth 2: -1.0 to 1.0
		areg `var' above media_std above_media_std if self_report_==1 & media_std>=-1.0 & media_std<=1.0, robust absorb(id_liceu_disc) cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB5a_bw2.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
		
		* Bandwidth 3: -0.5 to 0.5
		areg `var' above media_std above_media_std if self_report_==1 & media_std>=-.5 & media_std<=.5, robust absorb(id_liceu_disc) cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB5a_bw3.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append

		* CCT optimal bandwidth
		rdrobust `var' media_std if self_report_==1, bwselect(mserd) all kernel(tri) vce(cluster id_child2n) covs(`var'_dfe1)
		matrix B[1,`nreg'] =  e(tau_cl)
		*matrix B[2,`nreg'] =  e(se_tau_rb)
		matrix B[2,`nreg'] =  e(se_tau_cl)
		matrix B[3,`nreg'] =  e(pv_rb)
		*matrix B[3,`nreg'] =  e(pv_cl)
		matrix B[4,`nreg'] =  e(h_l)
		matrix B[5,`nreg'] =  e(N_h_l)
		matrix B[6,`nreg'] =  e(N_h_r) 
		qui tab id_child2 if media_std>=-e(b_r) & media_std<0
		matrix B[7,`nreg'] =  r(r)
		qui tab id_child2n if media_std>=0 & media_std<e(b_r)
		matrix B[8,`nreg'] =  r(r)
		sum `var' if media_std<=0 & media_std>=-e(h_r)
		matrix B[9,`nreg'] =  r(mean)
	
	local nreg = `nreg' + 1

	}
	
	preserve 
	
	drop Bs_disrupt Bs_confid Bs_effort Bs_ability Bs_perform

		svmat2 B
		keep if B1!=.
		keep B*
		
		export excel using "${Results}\Tables\TableB5_CCT.xls", firstrow(var) replace
	restore

	
********************************************************************************
******************************** TABLE B6 **************************************
********************************************************************************	
	
**** Table B6: Top vs. bottom classroom informants
	
foreach var of varlist better_class s_confid s_effort s_ability s_perform s_disrupt rosen_std bullying1c_std {
	reg `var' i.id_liceu_discn if reporter_higher==1 & self_report_==0, nocons
	predict `var'_dfe3
	
	reg `var' i.id_liceu_discn if reporter_lower==1 & self_report_==0, nocons
	predict `var'_dfe4
}

matrix B 	= J(9,12,.) 


	local TableB6a_bw1 "${Results}\Tables\TableB6a_bw1"
	local TableB6b_bw1 "${Results}\Tables\TableB6b_bw1"
	
	cap erase "`TableB6a_bw1'.xls"
	cap erase "`TableB6a_bw1'.txt"

	cap erase "`TableB6b_bw1'.xls"
	cap erase "`TableB6b_bw1'.txt"
	
	local TableB6a_bw2 "${Results}\Tables\TableB6a_bw2"
	local TableB6b_bw2 "${Results}\Tables\TableB6b_bw2"
	
	cap erase "`TableB6a_bw2'.xls"
	cap erase "`TableB6a_bw2'.txt"

	cap erase "`TableB6b_bw2'.xls"
	cap erase "`TableB6b_bw2'.txt"
	
	local TableB6a_bw3 "${Results}\Tables\TableB6a_bw3"
	local TableB6b_bw3 "${Results}\Tables\TableB6b_bw3"
	
	cap erase "`TableB6a_bw3'.xls"
	cap erase "`TableB6a_bw3'.txt"

	cap erase "`TableB6b_bw3'.xls"
	cap erase "`TableB6b_bw3'.txt"
	
	local nreg = 1

	foreach var of varlist better_class s_confid s_effort s_ability s_perform s_disrupt {
		* Panel A
		* Bandwidth 1: -1.5 to 1.5
		areg `var' above media_std above_media_std if reporter_higher==1 & self_report_==0 & media_std>=-1.5 & media_std<=1.5, absorb(id_liceu_disc) robust cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB6a_bw1.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
		
		* Bandwidth 2: -1.0 to 1.0
		areg `var' above media_std above_media_std if reporter_higher==1 & self_report_==0 & media_std>=-1 & media_std<=1, absorb(id_liceu_disc) robust cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB6a_bw2.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
		
		* Bandwidth 3: -0.5 to 0.5
		areg `var' above media_std above_media_std if reporter_higher==1 & self_report_==0 & media_std>=-.5 & media_std<=.5, absorb(id_liceu_disc) robust cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB6a_bw3.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append

		* CCT optimal bandwidth
		rdrobust `var' media_std if reporter_higher==1 & self_report_==0, bwselect(mserd) all kernel(tri) vce(cluster id_child2n) covs(`var'_dfe3)
		matrix B[1,`nreg'] =  e(tau_cl)
		*matrix B[2,`nreg'] =  e(se_tau_rb)
		matrix B[2,`nreg'] =  e(se_tau_cl)
		matrix B[3,`nreg'] =  e(pv_rb)
		*matrix B[3,`nreg'] =  e(pv_cl)
		matrix B[4,`nreg'] =  e(h_l)
		matrix B[5,`nreg'] =  e(N_h_l)
		matrix B[6,`nreg'] =  e(N_h_r) 
		qui tab id_child2 if media_std>=-e(b_r) & media_std<0
		matrix B[7,`nreg'] =  r(r)
		qui tab id_child2n if media_std>=0 & media_std<e(b_r)
		matrix B[8,`nreg'] =  r(r)
		sum `var' if media_std<=0 & media_std>=-e(h_r)
		matrix B[9,`nreg'] =  r(mean)
		
		
		** Panel B
		* Bandwidth 1: -1.5 to 1.5
		areg `var' above media_std above_media_std if reporter_lower==1 & self_report_==0 & media_std>=-1.5 & media_std<=1.5, absorb(id_liceu_disc) robust cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB6b_bw1.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
		
		* Bandwidth 2: -1.0 to 1.0
		areg `var' above media_std above_media_std if reporter_lower==1 & self_report_==0 & media_std>=-1 & media_std<=1, absorb(id_liceu_disc) robust cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB6b_bw2.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
		
		* Bandwidth 3: -0.5 to 0.5
		areg `var' above media_std above_media_std if reporter_lower==1 & self_report_==0 & media_std>=-.5 & media_std<=.5, absorb(id_liceu_disc) robust cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB6b_bw3.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append

		* CCT optimal bandwidth
		rdrobust `var' media_std if reporter_lower==1 & self_report_==0, bwselect(mserd) all kernel(tri) vce(cluster id_child2n) covs(`var'_dfe4)
		matrix B[1,6+`nreg'] =  e(tau_cl)
		*matrix B[2,6+`nreg'] =  e(se_tau_rb)
		matrix B[2,6+`nreg'] =  e(se_tau_cl)
		matrix B[3,6+`nreg'] =  e(pv_rb)
		*matrix B[3,6+`nreg'] =  e(pv_cl)
		matrix B[4,6+`nreg'] =  e(h_l)
		matrix B[5,6+`nreg'] =  e(N_h_l)
		matrix B[6,6+`nreg'] =  e(N_h_r) 
		qui tab id_child2 if media_std>=-e(b_r) & media_std<0
		matrix B[7,6+`nreg'] =  r(r)
		qui tab id_child2n if media_std>=0 & media_std<e(b_r)
		matrix B[8,6+`nreg'] =  r(r)
		sum `var' if media_std<=0 & media_std>=-e(h_r)
		matrix B[9,6+`nreg'] =  r(mean)
		
	local nreg = `nreg' + 1
		
	}	
	
	preserve 
	
	drop Bs_disrupt Bs_confid Bs_effort Bs_ability Bs_perform

		svmat2 B
		keep if B1!=.
		keep B*
		
		export excel using "${Results}\Tables\TableB6_CCT.xls", firstrow(var) replace
	restore	
	
********************************************************************************
******************************** TABLE B7 **************************************
********************************************************************************	
	
**** Table B7: Self reported vs teacher rankings, binary rankings


	local TableB7a "${Results}\Tables\TableB7a"
	local TableB7b "${Results}\Tables\TableB7b"
	
	cap erase "`TableB7a'.xls"
	cap erase "`TableB7a'.txt"

	cap erase "`TableB7b'.xls"
	cap erase "`TableB7b'.txt"
	
	foreach var of varlist better_class s_confid_br s_effort_br s_ability_br s_perform_br s_disrupt_br {
		* Panel A
		areg `var' above media_std above_media_std if self_report_==1, robust absorb(id_liceu_disc) cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB7a.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
		
		* Panel B
		areg `var' above media_std above_media_std if informants==4, absorb(id_liceu_disc) robust cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB7b.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
		
	}	
	
**** Absolute measures of student's perceptions
		
	foreach var of varlist rosen_std bullying1c_std {
		* Panel A, columns (7) and (8)
		areg `var' above media_std above_media_std if self_report_==1, robust absorb(id_liceu_disc) cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB7a.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
				
	}		
		
		
********************************************************************************
******************************** TABLE B8 **************************************
********************************************************************************	

**** Table B8: RD estimates of peer perceptions: ranks collapsed to binary outcomes
	
	local TableB8a "${Results}\Tables\TableB8a"
	local TableB8b "${Results}\Tables\TableB8b"
	
	cap erase "`TableB8a'.xls"
	cap erase "`TableB8a'.txt"

	cap erase "`TableB8b'.xls"
	cap erase "`TableB8b'.txt"
	
	foreach var of varlist better_class s_confid_br s_effort_br s_ability_br s_perform_br s_disrupt_br {
		* Panel A
		areg `var' above media_std above_media_std if reporter_higher==1 & self_report_==0, absorb(id_liceu_disc) robust cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB8a.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
		
		* Panel B
		areg `var' above media_std above_media_std if reporter_lower==1 & self_report_==0, absorb(id_liceu_disc) robust cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB8b.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
		
	}

********************************************************************************
******************************** TABLE B9 **************************************
********************************************************************************	

**** Table B9: Self-perceptions relative to teacher perceptions: ranks collapsed 
*			to binary outcomes (relative to teachers)

	local TableB9a "${Results}\Tables\TableB9a"
	local TableB9b "${Results}\Tables\TableB9b"
	
	cap erase "`TableB9a'.xls"
	cap erase "`TableB9a'.txt"
	
	cap erase "`TableB9b'.xls"
	cap erase "`TableB9b'.txt"

foreach var of varlist s_confid_br s_effort_br s_ability_br s_perform_br s_disrupt_br {
		
		* Panel A
		areg `var' i.informants34, robust absorb(id_child) cluster(id_child2)
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB9a.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
		
		* Panel B
		areg `var' i.informants34##c.media_std, robust absorb(id_child) cluster(id_child2)
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB9b.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
				
	}

	
********************************************************************************
******************************** TABLE B10 *************************************
********************************************************************************	
		
**** Table B10: Self reported vs teacher rankings, grade-9 only

	local TableB10a "${Results}\Tables\TableB10a"
	local TableB10b "${Results}\Tables\TableB10b"
	
	cap erase "`TableB10a'.xls"
	cap erase "`TableB10a'.txt"

	cap erase "`TableB10b'.xls"
	cap erase "`TableB10b'.txt"
	
	foreach var of varlist better_class s_confid s_effort s_ability s_perform s_disrupt {
		* Panel A
		areg `var' above media_std above_media_std if self_report_==1 & clasa_2==9, robust absorb(id_liceu_disc) cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB10a.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
		
		* Panel B
		areg `var' above media_std above_media_std if informants==4 & clasa_2==9, absorb(id_liceu_disc) robust cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB10b.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
		
	}	
	
**** Absolute measures of student's perceptions
	
	foreach var of varlist rosen_std bullying1c_std {
		* Panel A, columns (7) and (8)
		areg `var' above media_std above_media_std if self_report_==1 & clasa_2==9, robust absorb(id_liceu_disc) cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB10a.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
				
	}	
	
	
********************************************************************************
******************************** TABLE B11 *************************************
********************************************************************************	

**** Table B11: Top vs. bottom classroom informants, grade-9 only
	
	local TableB11a "${Results}\Tables\TableB11a"
	local TableB11b "${Results}\Tables\TableB11b"
	
	cap erase "`TableB11a'.xls"
	cap erase "`TableB11a'.txt"

	cap erase "`TableB11b'.xls"
	cap erase "`TableB11b'.txt"
	
	foreach var of varlist better_class s_confid s_effort s_ability s_perform s_disrupt {
		* Panel A
		areg `var' above media_std above_media_std if reporter_higher==1 & self_report_==0 & clasa_2==9, absorb(id_liceu_disc) robust cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB11a.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
		
		* Panel B
		areg `var' above media_std above_media_std if reporter_lower==1 & self_report_==0 & clasa_2==9, absorb(id_liceu_disc) robust cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB11b.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
		
	}	
	

********************************************************************************
******************************** TABLE B12 *************************************
********************************************************************************

**** Table B12: Self-perceptions relative to teacher perceptions grade 9 only

	local TableB12a "${Results}\Tables\TableB12a"
	local TableB12b "${Results}\Tables\TableB12b"
	
	cap erase "`TableB12a'.xls"
	cap erase "`TableB12a'.txt"
	
	cap erase "`TableB12b'.xls"
	cap erase "`TableB12b'.txt"

foreach var of varlist s_confid s_effort s_ability s_perform s_disrupt {
		
		* Panel A
		areg `var' i.informants34 if clasa_2==9, robust absorb(id_child) cluster(id_child2)
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB12a.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
		
		* Panel B
		areg `var' i.informants34##c.media_std if clasa_2==9, robust absorb(id_child) cluster(id_child2)
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB12b.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
				
	}
	

********************************************************************************
******************************** TABLE B13 *************************************
********************************************************************************
	
**** Table B14: Self reported vs teacher rankings, complete ranks

	gen Bbetter_class = 1 if Bs_disrupt==1 & Bs_confid==1 & Bs_effort==1 & Bs_ability==1 & Bs_perform==1

	local TableB13a "${Results}\Tables\TableB13a"
	local TableB13b "${Results}\Tables\TableB13b"
	
	cap erase "`TableB13a'.xls"
	cap erase "`TableB13a'.txt"

	cap erase "`TableB13b'.xls"
	cap erase "`TableB13b'.txt"
	
	foreach var of varlist better_class s_confid s_effort s_ability s_perform s_disrupt {
		* Panel A
		areg `var' above media_std above_media_std if self_report_==1 & B`var'==1, robust absorb(id_liceu_disc) cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB13a.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
		
		* Panel B
		areg `var' above media_std above_media_std if informants==4 & B`var'==1, absorb(id_liceu_disc) robust cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB13b.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
		
	}	
		
**** Absolute measures of student's perceptions
	
	foreach var of varlist rosen_std bullying1c_std {
		* Panel A, columns (7) and (8)
		areg `var' above media_std above_media_std if self_report_==1 & Bbetter_class==1, robust absorb(id_liceu_disc) cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB13a.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
				
	}	

	
********************************************************************************
******************************** TABLE B14 *************************************
********************************************************************************

**** Table B15: Top vs. bottom classroom informants, complete ranks
	
	local TableB14a "${Results}\Tables\TableB14a"
	local TableB14b "${Results}\Tables\TableB14b"
	
	cap erase "`TableB14a'.xls"
	cap erase "`TableB14a'.txt"

	cap erase "`TableB14b'.xls"
	cap erase "`TableB14b'.txt"
	
	foreach var of varlist better_class s_confid s_effort s_ability s_perform s_disrupt {
		* Panel A
		areg `var' above media_std above_media_std if reporter_higher==1 & self_report_==0 & B`var'==1, absorb(id_liceu_disc) robust cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB14a.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
		
		* Panel B
		areg `var' above media_std above_media_std if reporter_lower==1 & self_report_==0 & B`var'==1, absorb(id_liceu_disc) robust cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB14b.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
		
	}
	
		
********************************************************************************
******************************** TABLE B15 *************************************
********************************************************************************

**** Table B12: Self-perceptions relative to teacher perceptions, complete ranks

	local TableB15a "${Results}\Tables\TableB15a"
	local TableB15b "${Results}\Tables\TableB15b"
	
	cap erase "`TableB15a'.xls"
	cap erase "`TableB15a'.txt"
	
	cap erase "`TableB15b'.xls"
	cap erase "`TableB15b'.txt"

foreach var of varlist s_confid s_effort s_ability s_perform s_disrupt {
		
		* Panel A
		areg `var' i.informants34 if B`var'==1, robust absorb(id_child) cluster(id_child2)
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB15a.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
		
		* Panel B
		areg `var' i.informants34##c.media_std if B`var'==1, robust absorb(id_child) cluster(id_child2)
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableB15b.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
				
	}
	
	

********************************************************************************
********************************************************************************
************************** TABLES IN APPENDIX C ********************************
********************************************************************************
********************************************************************************


********************************************************************************
******************************** TABLE C1 **************************************
********************************************************************************

**** Table C1: Gender differences in RD estimates of self-perceptions and teacher perceptions

	local TableC1a "${Results}\Tables\TableC1a"
	local TableC1b "${Results}\Tables\TableC1b"

	cap erase "`TableC1a'.xls"
	cap erase "`TableC1a'.txt"

	cap erase "`TableC1b'.xls"
	cap erase "`TableC1b'.txt"
	
	foreach var of varlist better_class s_confid s_effort s_ability s_perform s_disrupt {
			* Self-reports
			areg `var' (i.above c.media_std c.above_media_std)##i.girl if self_report_==1, robust absorb(id_liceu_disc) cluster(id_child2) 
			quietly summ `var' if e(sample)==1
			outreg2 using "${Results}\Tables\TableC1a.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
		
			* Teacher reports
			areg `var' (i.above c.media_std c.above_media_std)##i.girl if informants==4, absorb(id_liceu_disc) robust cluster(id_child2) 
			quietly summ `var' if e(sample)==1
			outreg2 using "${Results}\Tables\TableC1b.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
		
	}	
	
**** Absolute measures of student's perceptions	
	
	foreach var of varlist rosen_std bullying1c_std {
		* Table C1
		areg `var' (i.above c.media_std c.above_media_std)##i.girl if self_report_==1, robust absorb(id_liceu_disc) cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableC1a.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append	
	}	
	
	
********************************************************************************
******************************** TABLE C2 **************************************
********************************************************************************	
	
**** Table C2: Gender differences in RD estimates of peer perceptions

	local TableC2a "${Results}\Tables\TableC2a"
	local TableC2b "${Results}\Tables\TableC2b"
	
	cap erase "`TableC2a'.xls"
	cap erase "`TableC2a'.txt"

	cap erase "`TableC2b'.xls"
	cap erase "`TableC2b'.txt"
	
	foreach var of varlist better_class s_confid s_effort s_ability s_perform s_disrupt {
		
			* Top classroom reporters
			areg `var' (i.above c.media_std c.above_media_std)##i.girl if reporter_higher==1 & self_report_==0, absorb(id_liceu_disc) robust cluster(id_child2) 
			quietly summ `var' if e(sample)==1
			outreg2 using "${Results}\Tables\TableC2a.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
			
			* Bottom classroom reporters
			areg `var' (i.above c.media_std c.above_media_std)##i.girl if reporter_lower==1 & self_report_==0, absorb(id_liceu_disc) robust cluster(id_child2) 
			quietly summ `var' if e(sample)==1
			outreg2 using "${Results}\Tables\TableC2b.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
		
	}	

	
********************************************************************************
******************************** TABLE C3 **************************************
********************************************************************************

**** Table C3: Gender differences in self-perceptions relative to teacher perceptions

	local TableC3a "${Results}\Tables\TableC3a"
	local TableC3b "${Results}\Tables\TableC3b"
	
	cap erase "`TableC3a'.xls"
	cap erase "`TableC3a'.txt"
	
	cap erase "`TableC3b'.xls"
	cap erase "`TableC3b'.txt"	
	
	foreach var of varlist better_class s_confid s_effort s_ability s_perform s_disrupt {
			* Panel A
			areg `var' i.informants34##i.girl, robust cluster(id_child2) absorb(id_liceu_disc)
			quietly summ `var' if e(sample)==1
			outreg2 using "${Results}\Tables\TableC3a.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
			
			* Panel B		
			areg `var' i.informants34##c.media_std##i.girl, robust cluster(id_child2) absorb(id_liceu_disc)
			quietly summ `var' if e(sample)==1
			outreg2 using "${Results}\Tables\TableC3b.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
		
	}		

	
********************************************************************************
******************************** TABLE C4 **************************************
********************************************************************************
	
	gen later_grade = (clasa_2==11 | clasa_2==12)

**** Table C4: Early vs late grades differences in RD estimates of self-perceptions and teacher perceptions

	local TableC4a "${Results}\Tables\TableC4a"
	local TableC4b "${Results}\Tables\TableC4b"

	cap erase "`TableC4a'.xls"
	cap erase "`TableC4a'.txt"
	
	cap erase "`TableC4b'.xls"
	cap erase "`TableC4b'.txt"

	
	foreach var of varlist better_class s_confid s_effort s_ability s_perform s_disrupt {
		
			* Self-reports
			areg `var' (i.above c.media_std c.above_media_std)##i.later_grade if self_report_==1, robust absorb(id_liceu_disc) cluster(id_child2) 
			quietly summ `var' if e(sample)==1
			outreg2 using "${Results}\Tables\TableC4a.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2100) append
		
			* Teacher reports
			areg `var' (i.above c.media_std c.above_media_std)##i.later_grade if informants==4, absorb(id_liceu_disc) robust cluster(id_child2) 
			quietly summ `var' if e(sample)==1
			outreg2 using "${Results}\Tables\TableC4b.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2100) append
		
	}	
	
**** Absolute measures of student's perceptions
	
	foreach var of varlist rosen_std bullying1c_std {

		areg `var' (i.above c.media_std c.above_media_std)##i.later_grade if self_report_==1, robust absorb(id_liceu_disc) cluster(id_child2) 
		quietly summ `var' if e(sample)==1
		outreg2 using "${Results}\Tables\TableC4a.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2100) append	
	}	
	

********************************************************************************
******************************** TABLE C5 **************************************
********************************************************************************

**** Table C5: Early vs late grades differences in RD estimates of peer perceptions

	local TableC5a "${Results}\Tables\TableC5a"
	local TableC5b "${Results}\Tables\TableC5b"
	
	cap erase "`TableC5a'.xls"
	cap erase "`TableC5a'.txt"

	cap erase "`TableC5b'.xls"
	cap erase "`TableC5b'.txt"
	
	foreach var of varlist better_class s_confid s_effort s_ability s_perform s_disrupt{
		
			* Top classroom reporters
			areg `var' (i.above c.media_std c.above_media_std)##i.later_grade if reporter_higher==1 & self_report_==0, absorb(id_liceu_disc) robust cluster(id_child2) 
			quietly summ `var' if e(sample)==1
			outreg2 using "${Results}\Tables\TableC5a.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2100) append
			
			* Bottom classroom reporters
			areg `var' (i.above c.media_std c.above_media_std)##i.later_grade if reporter_lower==1 & self_report_==0, absorb(id_liceu_disc) robust cluster(id_child2) 
			quietly summ `var' if e(sample)==1
			outreg2 using "${Results}\Tables\TableC5b.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2100) append
		
	}
	

********************************************************************************
******************************** TABLE C6 **************************************
********************************************************************************
	
**** Table C6: Early vs late grades differences in self-perceptions relative to teacher perceptions

	local TableC6a "${Results}\Tables\TableC6a"
	local TableC6b "${Results}\Tables\TableC6b"
	
	cap erase "`TableC6a'.xls"
	cap erase "`TableC6a'.txt"

	cap erase "`TableC6b'.xls"
	cap erase "`TableC6b'.txt"
	
	foreach var of varlist better_class s_confid s_effort s_ability s_perform s_disrupt{
			* Panel A
			areg `var' i.informants34##i.later_grade, robust cluster(id_child2) absorb(id_liceu_disc)
			quietly summ `var' if e(sample)==1
			outreg2 using "${Results}\Tables\TableC6a.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2000) append
			
			* Panel B			
			areg `var' i.informants34##c.media_std##i.later_grade, robust cluster(id_child2) absorb(id_liceu_disc)
			quietly summ `var' if e(sample)==1
			outreg2 using "${Results}\Tables\TableC6b.xls", se bracket coefastr addstat(Mean, r(mean)) bdec(4) sdec(4) excel slow(2100) append
				
	}		
	
************************************************************************************************************************************************************************************************************************************************
************************************************************************************************************************************************************************************************************************************************
************************************************************************************************************************************************************************************************************************************************
************************************************************************************************************************************************************************************************************************************************
************************************************************************************************************************************************************************************************************************************************
************************************************************************************************************************************************************************************************************************************************